﻿using Domain.Model;
using Domain.Model.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    /// <summary>
    /// 奖金管理
    /// </summary>
    public class BonusController : LoginedController
    {

        //
        // GET: /Bonus/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult IndexPage(int pageIndex, int pageSize, string keyword)
        {
            if (keyword == null) keyword = "";

            var list = DbContext.Bonus.Where(i => i.No.ToString().Contains(keyword) || i.Settlementer.Contains(keyword)).OrderByDescending(i => i.No).ToPage(pageIndex, pageSize);

            return this.PageResult(list.TotalCount, this.ViewHtml(list));
        }
        //结算
        public ActionResult Settlement()
        {
            var model = new Bonus();
            var reslut = DbContext.Bonus.OrderByDescending(i => i.No).FirstOrDefault();
            if (reslut != null)
            {
                model.No = reslut.No + 1;
                model.BeginTime = reslut.EndTime;
            }
            else
            {
                model.No = 1;
                model.BeginTime = DateTime.Now;
            }

            model.Settlementer = LoginUser.NickName;
            model.State = State.未结算;
            return View(model);
        }

        [HttpPost]
        public ActionResult Settlement(Bonus model)
        {
            DbContext.Bonus.Add(model);
            DbContext.SaveChanges();
            return Json(new { state=true,value="index/"});
        }
        public ActionResult SettlementState(Guid id)
        {
            var model = DbContext.Bonus.Find(id);
            model.State = State.已结算;
            DbContext.SaveChanges();
            return Json(new { state = true, value = "/index/" });
        }
        public ActionResult SettlementGrant(Guid id)
        {
            var model = DbContext.Bonus.Find(id);
            model.State = State.已发放;
            DbContext.SaveChanges();
            return Json(new { state = true, value = "/index/" });
        }
        public ActionResult BonusDetail(int id)
        {
            var model = new BonusDetail();
            model.No = id;
            return View(model);
        }
        public ActionResult BonusDetailPage(int id, int pageIndex, int pageSize, string keyword)
        {
            var list = DbContext.BonusDetail.Where(i => i.No == id && i.No.ToString().Contains(keyword)).OrderByDescending(i => i.No).ToPage(pageIndex, pageSize);
            return this.PageResult(list.TotalCount, this.ViewHtml(list));
        }
        public ActionResult BonusDel(Guid id)
        {
            var model= DbContext.Bonus.Find(id);
            DbContext.Bonus.Remove(model);
            DbContext.SaveChanges();
            return Json(new { state = true, value = "/index/" });
        }
    }
}
